package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyCredentialAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.MyCertificateEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.tool.notchtool.NotchTools;
import com.runen.infinitemovement.tool.notchtool.core.NotchProperty;
import com.runen.infinitemovement.tool.notchtool.core.OnNotchCallBack;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.infinitemovement.widget.popupwind.MyCertificatePoppupWindow;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyCredentialActivity extends BaseActivity {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.rci_my_header)
    RoundedCornersImage rciMyHeader;
    @BindView(R.id.tv_my_name)
    TextView tvMyName;
    @BindView(R.id.ll_title)
    LinearLayout ll_title;
    @BindView(R.id.tv_company_name)
    TextView tvCompanyName;
    @BindView(R.id.rv_my_credential_list)
    RecyclerView rvMyCredentialList;
    private MyCredentialAdapter credentialAdapter;
    private String nickname;
    private String headImage;
    private String companyName;
    private String companyLogo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_credential;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);

        //适配刘海屏
        NotchTools.getFullScreenTools().translucentStatusBar(this, new OnNotchCallBack() {
            @Override
            public void onNotchPropertyCallback(NotchProperty notchProperty) {
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ll_title.getLayoutParams();
                layoutParams.topMargin = CommonUtil.getStatusBarHeight(MyCredentialActivity.this);
                ll_title.setLayoutParams(layoutParams);
            }
        });

        LinearLayoutManager layoutManager = new GridLayoutManager(this, 3);
        rvMyCredentialList.setLayoutManager(layoutManager);
        credentialAdapter = new MyCredentialAdapter(this);
        rvMyCredentialList.setAdapter(credentialAdapter);
        initDada();
        credentialAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (credentialAdapter.getData().get(position).got){
                    showPopup(credentialAdapter.getData().get(position));
                }
            }
        });
    }

    private void showPopup(MyCertificateEntity.DataBean.CertificatesBean certificatesBean) {
        MyCertificatePoppupWindow poppupWindow = MyCertificatePoppupWindow.getInstance(this,
                nickname, headImage, companyLogo, companyName, certificatesBean);
        poppupWindow.show();
    }

    private void initDada() {
        showDialog();
        new UserAPI().getMyCertificate(new BaseResultCallback<MyCertificateEntity>() {
            @Override
            public void onResponse(MyCertificateEntity response) {
                dismissDialog();
                if (response.code == 0) {
                    if (response.data != null) {
                        companyLogo = response.data.topCompanyLogo;
                        nickname = response.data.nickname;
                        companyName = response.data.topCompanyName;
                        headImage = response.data.headImage;
                        tvMyName.setText(nickname);
                        GlideUtils.loadImage(MyCredentialActivity.this, headImage, rciMyHeader);
                        tvCompanyName.setText(companyName);
                        if (response.data.certificates != null && response.data.certificates.size() > 0) {
                            credentialAdapter.setNewData(response.data.certificates);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @OnClick(R.id.top_btn_left)
    public void onViewClicked() {
        finish();
    }
}
